//
//  CTOfficialMessageView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/10.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTOfficialMessageView.h"
#import "CTDebugManager.h"
#import "CTHeader.h"
#import "CTOfficialMessageCell.h"
#import "CTUserItem.h"
#import "CTUser.h"
#import "CTFile.h"
#import "CTMessageItems.h"
#import "CTDataGenerator.h"
#import "CTAppLanguage.h"


@interface CTOfficialMessageView ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * officialMesgTableView;
    
    NSArray * officialMessageArray;
    
    UIView * noDataView;
}

@end




@implementation CTOfficialMessageView
@synthesize delegate;


- (void)dealloc
{
    [noDataView removeFromSuperview];
    noDataView = nil;
    
    [officialMesgTableView removeFromSuperview];
    officialMesgTableView = nil;
    
  
    delegate = nil;
    
    [CTDebugManager showErrorLogInfo:@"dealloc : CTOfficialMessageView Is Removed!!!!"];
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.frame = CGRectMake(0, 64, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT - 64 - 50);
        self.backgroundColor = [UIColor clearColor];
        
        [self createSubviews];
        [self creatNoDataView];
    }
    return self;
}



- (void)createSubviews
{
    officialMesgTableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
    officialMesgTableView.delegate = self;
    officialMesgTableView.dataSource = self;
    officialMesgTableView.showsVerticalScrollIndicator = NO;
    officialMesgTableView.rowHeight = 68.0f;
    officialMesgTableView.backgroundColor = [UIColor redColor];
    officialMesgTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    officialMesgTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    officialMesgTableView.backgroundColor = [UIColor clearColor];
    [officialMesgTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self addSubview:officialMesgTableView];
    
}

-(void)creatNoDataView
{
    
    noDataView = [[UIView alloc] initWithFrame:CGRectMake(0,(CT_HEADER_SCREEN_HEIGHT - 64-50 - 184)/2.0, CT_HEADER_SCREEN_WIDTH, 184)];
    noDataView.backgroundColor = [UIColor clearColor];
 //   noDataView.center = self.center;
    noDataView.hidden = YES;
    [self addSubview:noDataView];
    
    UIImageView * notLoginInImageView = [[UIImageView alloc] initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH - 90.5)/2.0, 0, 90.5, 108)];
    notLoginInImageView.image = [UIImage imageNamed:@"wuneirong"];
    [noDataView addSubview:notLoginInImageView];
    
    UILabel * remindLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,notLoginInImageView.frame.size.height + 15.0, CT_HEADER_SCREEN_WIDTH, 18.0)];
    remindLabel.textAlignment = NSTextAlignmentCenter;
    remindLabel.font = [UIFont systemFontOfSize:18.0];
    remindLabel.text = [CTAppLanguage localizedStringForKey:@"message_no_mesg" withValue:nil withTable:@"Language"];
    remindLabel.textColor = [UIColor colorWithRed:164/255.0 green:164/255.0 blue:164/255.0 alpha:1.0];
    [noDataView addSubview:remindLabel];
    
  
}




#pragma mark-UITableViewDataSource
//分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return officialMessageArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    CTOfficialMessageCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[CTOfficialMessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    cell.item = [officialMessageArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    tableView.rowHeight = cell.bounds.size.height;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    CTOfficialMessageItem * itm = [officialMessageArray objectAtIndex:indexPath.row];
    [self cancelIsReadAction:itm];

    
    if ([self.delegate respondsToSelector:@selector(selectOfficialMessgaeAction:)]) {
        [self.delegate selectOfficialMessgaeAction:[officialMessageArray objectAtIndex:indexPath.row]];
    }
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(deleteOfficialMessageAction:)]) {
        [self.delegate deleteOfficialMessageAction:[officialMessageArray objectAtIndex:indexPath.row]];
    }
}


#pragma mark - action -

-(void)cancelIsReadAction:(CTOfficialMessageItem *)itm
{
    if ([itm.isRead isEqualToString:@"1"]) {
        return;
    }
    
    NSString * fileNameStr;
    CTUserItem * userItem = [CTUser userInfo];
    if (userItem != nil)fileNameStr = [NSString stringWithFormat:@"%@.plist",userItem.uid];
    else fileNameStr = @"visitor.plist";
    NSMutableArray * infoMutableArr = [CTFile getArrFile:fileNameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/officialMesg"];
    
    if (infoMutableArr != nil || infoMutableArr.count != 0) {
        
        for (NSMutableDictionary * infoDic in infoMutableArr) {
            NSString * noticeID = [NSString stringWithFormat:@"%@",[[infoDic objectForKey:@"extras"] objectForKey:@"notice_id"]];
            
            if ([itm.extras.notice_id isEqualToString:noticeID]) {
                [infoDic setObject:@"1" forKey:@"isRead"];
                break;
            }
        }
    }
    [CTFile cacheArrFile:infoMutableArr fileName:fileNameStr saveDoc:CTFileDocLibrary savePath:@"cacheData/message/officialMesg"];
    officialMessageArray = [CTDataGenerator generateOfficialMessageListItems];
    [officialMesgTableView reloadData];
}


#pragma mark -setter -

-(void)setMessageListArr:(NSArray *)listArr
{
    officialMessageArray = listArr;
    if (officialMessageArray.count == 0) {
        noDataView.hidden = NO;
    }else{
        noDataView.hidden = YES;
    }
    
    [officialMesgTableView reloadData];
}

-(void)setViewHide:(BOOL)hideView
{
    
    self.hidden = hideView;
}



@end
